package com.tg.app.view;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.tg.app.R;
import com.tg.app.camera.AVIOCTRLDEFs;

public class CameraPlayerPtzView extends LinearLayout {
    private ImageView ptzDirection, ptzDirectionLeft, ptzDirectionRight, ptzDirectionUp, ptzDirectionDown;
    public CameraPlayerPtzView(Context context) {
        super(context);
        init(context);
    }

    public CameraPlayerPtzView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CameraPlayerPtzView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_camera_player_ptz,this);
        ptzDirection = view.findViewById(R.id.camera_player_ptz_direction);
        ptzDirectionDown = view.findViewById(R.id.camera_player_ptz_direction_down);
        ptzDirectionUp = view.findViewById(R.id.camera_player_ptz_direction_up);
        ptzDirectionLeft = view.findViewById(R.id.camera_player_ptz_direction_left);
        ptzDirectionRight = view.findViewById(R.id.camera_player_ptz_direction_right);
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            float top = getResources().getDimension(R.dimen.camera_player_ptz_direction_margin);
            ConstraintLayout.LayoutParams ptzParamsUP = (ConstraintLayout.LayoutParams) ptzDirectionUp.getLayoutParams();
            ptzParamsUP.topMargin = (int) top;
            ptzDirectionUp.setLayoutParams(ptzParamsUP);
            ConstraintLayout.LayoutParams ptzParamsDown = (ConstraintLayout.LayoutParams) ptzDirectionDown.getLayoutParams();
            ptzParamsDown.bottomMargin = (int) top;
            ptzDirectionDown.setLayoutParams(ptzParamsDown);
        }else {
            float top = getResources().getDimension(R.dimen.camera_player_ptz_direction_margin_land);
            ConstraintLayout.LayoutParams ptzParamsUP = (ConstraintLayout.LayoutParams) ptzDirectionUp.getLayoutParams();
            ptzParamsUP.topMargin = (int) top;
            ptzDirectionUp.setLayoutParams(ptzParamsUP);
            ConstraintLayout.LayoutParams ptzParamsDown = (ConstraintLayout.LayoutParams) ptzDirectionDown.getLayoutParams();
            ptzParamsDown.bottomMargin = (int) top;
            ptzDirectionDown.setLayoutParams(ptzParamsDown);
        }
    }

    public void hide() {
        ptzDirection.setVisibility(GONE);
        ptzDirectionDown.setVisibility(GONE);
        ptzDirectionUp.setVisibility(GONE);
        ptzDirectionLeft.setVisibility(GONE);
        ptzDirectionRight.setVisibility(GONE);
    }
    public void direction(int control, boolean small) {

        if (small) {
            ptzDirection.setVisibility(GONE);
            ptzDirectionLeft.setVisibility(GONE);
            ptzDirectionRight.setVisibility(GONE);
            ptzDirectionUp.setVisibility(GONE);
            ptzDirectionDown.setVisibility(GONE);
            if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_LEFT) {
                ptzDirectionLeft.setVisibility(VISIBLE);
            } else if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_RIGHT) {
                ptzDirectionRight.setVisibility(VISIBLE);
            } else if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_DOWN) {
                ptzDirectionDown.setVisibility(VISIBLE);
            } else if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_UP) {
                ptzDirectionUp.setVisibility(VISIBLE);
            }
        } else {
            if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_LEFT) {
                ptzDirection.setImageResource(R.mipmap.camera_player_ptz_direction_left);
            } else if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_RIGHT) {
                ptzDirection.setImageResource(R.mipmap.camera_player_ptz_direction_right);
            } else if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_DOWN) {
                ptzDirection.setImageResource(R.mipmap.camera_player_ptz_direction_down);
            } else if (control == AVIOCTRLDEFs.AVIOCTRL_PTZ_UP) {
                ptzDirection.setImageResource(R.mipmap.camera_player_ptz_direction_up);
            }
            ptzDirection.setVisibility(VISIBLE);

        }

    }
}
